package cn.stronglink.asset.module.assetmanage.pulloff.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.model.PullOff;
import cn.stronglink.asset.module.assetmanage.pulloff.service.PullOffService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.WebUtil;

@Controller
@RequestMapping(value = "pulloff")
public class PullOffController extends AbstractController {

	@Autowired
	private PullOffService pullOffService;

	// 查询所有下架单
	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryPullOffList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {
		boolean isAdmin = (boolean) request.getSession().getAttribute("isAdmin");
		if(!isAdmin) {
			params.put("operatorBy", WebUtil.getCurrentUser());
		}
		@SuppressWarnings("unchecked")
		Page<PullOff> page = (Page<PullOff>) super.getPage(params);
		Page<PullOff> data = pullOffService.queryPullOffList(page,params);
		return setSuccessModelMap(modelMap, data);
	}
	
	// 根据下架单Id查询明细
	@RequestMapping(value = "qryById")
	public ResponseEntity<ModelMap> queryPullOffDetailList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Long id) {	
		PullOff data =pullOffService.queryPullOffById(id);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 生成上架下架单号
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param PullOff
	 * @return
	 */
	@RequestMapping(value = "getJoberNum")
	public ResponseEntity<ModelMap> getJoberNum(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		Long num =IdWorker.getId();
		return setSuccessModelMap(modelMap, num.toString());
	}
	
	
	/**
	 * 新增下架单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param PullOff
	 * @return
	 */
	@OperateLog(module = "下架管理",desc="添加下架单", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody PullOff pullOff) {
		pullOffService.create(pullOff);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 编辑领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param pullOff
	 * @return
	 */
	@OperateLog(module = "下架管理",desc="编辑下架单", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody PullOff pullOff) {
		pullOffService.edit(pullOff);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 执行下架
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@OperateLog(module = "下架管理",desc="确认下架", type = OpType.UPDATE)
	@RequestMapping(value = "doPullOff")
	public ResponseEntity<ModelMap> doPullOff(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody PullOff pullOff) {
		pullOffService.doPullOff(pullOff);
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 删除领用详情
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@OperateLog(module = "下架管理",desc="删除下架单", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		pullOffService.remove(id);
		return setSuccessModelMap(modelMap, null);
	}
	
	
//	/**
//	 * 领用申请分配
//	 * @param modelMap
//	 * @param request
//	 * @param response
//	 * @param id
//	 * @return
//	 */
//	@RequestMapping(value = "distribution")
//	public ResponseEntity<ModelMap> distribution(ModelMap modelMap, HttpServletRequest request,
//			HttpServletResponse response,@RequestBody UsingApplyDetail detail) {
//		usingService.distribution(detail);
//		return setSuccessModelMap(modelMap, null);
//	}
	
}
